<template>
  <div class="content">
    <div style="width:1000px">
      <div class="content_head">
        <el-button type="primary" size="mini" @click="visiable=true">添加</el-button>
      </div>
      <el-table :data="tableData" border style="margin-top:20px">
        <el-table-column label="id">
          <template slot-scope="scope">
            <span>{{ scope.row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column label="角色名称">
          <template slot-scope="scope">
            <span>{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间">
          <template slot-scope="scope">
            <span>{{ getDate(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="edit(scope.row)">编辑</el-button>
            <el-button type="danger" size="mini" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog title="添加角色" center :visible.sync="visiable" width="800px">
      <el-form :model="form" :rules="rules" ref="form" label-width="50px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入角色名称"></el-input>
        </el-form-item>
        <el-form-item label="权限" prop="permission">
          <el-checkbox-group v-model="form.permission">
            <el-checkbox
              v-for="(item,index) in permission"
              :key="index"
              :label="item.id"
            >{{item.name}}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" @click="submit('form')">添加</el-button>
          <el-button size="small" @click="reset('form')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog title="编辑角色" center :visible.sync="visiable1" width="800px">
      <el-form :model="role" :rules="rules" ref="form1" label-width="50px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="role.name" placeholder="请输入角色名称"></el-input>
        </el-form-item>
        <el-form-item label="权限" prop="permission">
          <el-checkbox-group v-model="role.permission">
            <el-checkbox
              v-for="(item,index) in permission"
              :key="index"
              :label="item.id"
            >{{item.name}}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" @click="submit1('form1')">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import {
  roleList,
  add,
  update,
  del,
  detail,
  permissionList
} from "@/api/role.js";
import { getDate } from "@/utils/dateUtil.js";
export default {
  data() {
    return {
      visiable1: false,
      role: {
        id: "",
        name: "",
        permission: []
      },
      rules: {
        name: [{ required: true, message: "请输入角色名称", trigger: "blur" }],
        permission: [
          {
            type: "array",
            required: true,
            message: "请选择",
            trigger: "blur"
          }
        ]
      },
      permission: [],
      tableData: [],
      visiable: false,
      form: {
        name: "",
        permission: []
      }
    };
  },
  mounted() {
    this.getTableData();
    this.getPermissionList();
  },
  methods: {
    submit1(name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          update(this.role.id, this.role.name, this.role.permission).then(
            res => {
              this.$message({
                type: "success",
                message: res.message
              });
              this.visiable1 = false;
              this.getTableData();
              this.$refs[name].resetFields();
            }
          );
        } else {
          return false;
        }
      });
    },
    submit(name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          add(this.form.name, this.form.permission).then(res => {
            this.$message({
              type: "success",
              message: res.message
            });
            this.visiable = false;
            this.getTableData();
            this.$refs[name].resetFields();
          });
        } else {
          return false;
        }
      });
    },
    reset(name) {
      this.$refs[name].resetFields();
    },
    getPermissionList() {
      permissionList().then(res => {
        console.log(res.data);
        this.permission = res.data;
      });
    },
    edit(data) {
      var that = this;
      detail(data.id).then(res => {
        that.role.id = data.id;
        that.role.name = data.name;
        that.role.permission = res.data.rolePermissions.map(
          (item, index, arr) => item.id
        );
        that.visiable1 = true;
      });
    },
    del(datd) {
      this.$confirm("此操作将删除该角色, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          del(id).then(() => {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.getTableData();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    getTableData() {
      roleList().then(res => {
        this.tableData = res.data;
      });
    },
    getDate(time) {
      return getDate(time);
    }
  }
};
</script>

<style scoped>
.content {
  display: flex;
  justify-content: center;
}
.content_head {
  width: 100%;
  display: flex;
  justify-content: flex-end;
}
</style>

